package com.tisson.action;

import java.util.ArrayList;
import java.util.List;

import com.tisson.bo.AreaBO;
import com.tisson.bo.DepartmentBO;
import com.tisson.comment.WebConstants;
import com.tisson.pojo.AcDepartment;
import com.tisson.pojo.Area;
import com.tisson.pojo.Page;

public class DepartmentAction extends ActionSup{
	
	private String departmentId;
	private String depName;
	private String depAdmin;
	private String depAddress;
	private String depPhone;
	private String logicMark0;
	private String logicMark1;
	private String logicMark2;
	private List result;
	private Page page;
	private String id;
	
	public String search(){
		DepartmentBO bo = new DepartmentBO();
		page = new Page();
		List all = new ArrayList();
		result = new ArrayList();
		try{
			all = bo.search(request, departmentId, depName, depAdmin, depAddress, depPhone, logicMark0, logicMark1, logicMark2);
		}
		catch(Exception e){
			e.printStackTrace();
			all = new ArrayList();
		}
		request.getSession().setAttribute("list", all);
		try{
			for(int i=0;i<WebConstants.PAGESIZE;i++){
				if(all.get(i)!=null){
					result.add(all.get(i));
				}
			}
		}catch(Exception e){
		}
		page.setCurrentPage(1);
		page.setPrePage(page.getCurrentPage()-1);
		page.setNextPage(page.getCurrentPage()+1);
		page.setListSize(all.size());
		page.setTotalPages(page.getListSize()%WebConstants.PAGESIZE==0?page.getListSize()/WebConstants.PAGESIZE:(page.getListSize()/WebConstants.PAGESIZE)+1);
		return "list";
	}
	
	public String list(){
		DepartmentBO bo = new DepartmentBO();
		page = new Page();
		List all = new ArrayList();
		result = new ArrayList();
		try{
			all = bo.findAll(request);
		}
		catch(Exception e){
			e.printStackTrace();
			all = new ArrayList();
		}
		request.getSession().setAttribute("list", all);
		try{
			for(int i=0;i<WebConstants.PAGESIZE;i++){
				if(all.get(i)!=null){
					result.add(all.get(i));
				}
			}
		}catch(Exception e){
		}
		page.setCurrentPage(1);
		page.setPrePage(page.getCurrentPage()-1);
		page.setNextPage(page.getCurrentPage()+1);
		page.setListSize(all.size());
		page.setTotalPages(page.getListSize()%WebConstants.PAGESIZE==0?page.getListSize()/WebConstants.PAGESIZE:(page.getListSize()/WebConstants.PAGESIZE)+1);
		return "list";
	}
	
	public String delete(){
		DepartmentBO bo = new DepartmentBO();
		String[] idList = request.getParameterValues("id");
		if(idList!=null){
			for(int i=0;i<idList.length;i++){
				AcDepartment department = new AcDepartment();
				department.setDepartmentId(idList[i]);
				bo.delete(department);
			}
		}
		return list();
	}
	
	public String add(){
		DepartmentBO bo = new DepartmentBO();
		AcDepartment department = new AcDepartment();
		department.setDepAddress(depAddress);
		department.setDepAdmin(depAdmin);
		department.setDepName(depName);
		department.setDepPhone(depPhone);
		bo.add(department);
		return list();
	}
	
	public String edit(){
		try{
			DepartmentBO bo = new DepartmentBO();
			AcDepartment department = bo.findById(id);
			depAddress = department.getDepAddress();
			depAdmin = department.getDepAdmin();
			departmentId = department.getDepartmentId();
			depName = department.getDepName();
			depPhone = department.getDepPhone();
		}catch(Exception e){
			e.printStackTrace();
		}
		return "editView";
	}
	
	public String update(){
		try{
			DepartmentBO bo = new DepartmentBO();
			AcDepartment department = new AcDepartment();
			department.setDepAddress(depAddress);
			department.setDepAdmin(depAdmin);
			department.setDepartmentId(departmentId);
			department.setDepName(depName);
			department.setDepPhone(depPhone);
			bo.update(department);
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return list();
	}
	
	public String getDepartmentId() {
		return departmentId;
	}
	public void setDepartmentId(String departmentId) {
		this.departmentId = departmentId;
	}
	public String getDepName() {
		return depName;
	}
	public void setDepName(String depName) {
		this.depName = depName;
	}
	public String getDepAdmin() {
		return depAdmin;
	}
	public void setDepAdmin(String depAdmin) {
		this.depAdmin = depAdmin;
	}
	public String getDepAddress() {
		return depAddress;
	}
	public void setDepAddress(String depAddress) {
		this.depAddress = depAddress;
	}
	public String getDepPhone() {
		return depPhone;
	}
	public void setDepPhone(String depPhone) {
		this.depPhone = depPhone;
	}
	public String getLogicMark0() {
		return logicMark0;
	}
	public void setLogicMark0(String logicMark0) {
		this.logicMark0 = logicMark0;
	}
	public String getLogicMark1() {
		return logicMark1;
	}
	public void setLogicMark1(String logicMark1) {
		this.logicMark1 = logicMark1;
	}
	public String getLogicMark2() {
		return logicMark2;
	}


	public void setLogicMark2(String logicMark2) {
		this.logicMark2 = logicMark2;
	}

	public List getResult() {
		return result;
	}
	public void setResult(List result) {
		this.result = result;
	}
	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	

}
